package com.bianguo.android.bprinter.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.bprinter.R;

public class ZoomFrameView extends View {

    public static final int ICON_LEFT_TOP = 0;
    public static final int ICON_RIGHT_TOP = 1;
    public static final int ICON_RIGHT_BOTTOM = 2;
    public static final int ICON_LEFT_BOTTOM = 3;

    public static final int CLICK_RECT = 40;
    private Paint mPaint;
    private RectF mRectF;
    private Bitmap scaleBitmap, deleteBitmap, cutBitmap, mirrorBitmap;
    private int mWidth, mHeight;
    private long clickTime;
    private int lineColor;
    private int leftTopImgRes, leftBottomImgRes, rightTopImgRes, rightBottomImgRes;
    private int lineWidth;
    private OnClickIconListener onClickIconListener;

    public ZoomFrameView(Context context) {
        this(context, null);
    }

    public ZoomFrameView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomFrameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DBoardView);
        lineColor = typedArray.getColor(R.styleable.ZoomFrameView_rect_color, ActivityCompat.getColor(context, R.color.colorLine));
        leftTopImgRes = typedArray.getResourceId(R.styleable.ZoomFrameView_left_top_img, R.drawable.zoom_delete);
        leftBottomImgRes = typedArray.getResourceId(R.styleable.ZoomFrameView_left_bottom_img, R.drawable.zoom_cut);
        rightTopImgRes = typedArray.getResourceId(R.styleable.ZoomFrameView_right_top_img, R.drawable.zoom_mirror);
        rightBottomImgRes = typedArray.getResourceId(R.styleable.ZoomFrameView_right_bottom_img, R.drawable.zoom_scale);
        lineWidth = typedArray.getInteger(R.styleable.ZoomFrameView_rect_width, 2);
        typedArray.recycle();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(lineWidth);
        mPaint.setColor(lineColor);

        mRectF = new RectF();
        scaleBitmap = BitmapFactory.decodeResource(context.getResources(), rightBottomImgRes);
        deleteBitmap = BitmapFactory.decodeResource(context.getResources(), leftTopImgRes);
        mirrorBitmap = BitmapFactory.decodeResource(context.getResources(), rightTopImgRes);
        cutBitmap = BitmapFactory.decodeResource(context.getResources(), leftBottomImgRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getX() >= getRight() - CLICK_RECT && event.getX() <= getRight() &&
                        event.getY() >= getTop() && event.getY() <= getTop() + CLICK_RECT) {
                    //右上角 镜像
                    clickTime = System.currentTimeMillis();
                    return true;
                }
                if (event.getX() >= getLeft() && event.getX() <= getLeft() + CLICK_RECT &&
                    event.getY() >= getTop() && event.getY() <= getTop() + CLICK_RECT) {
                    //左上角 删除
                    clickTime = System.currentTimeMillis();
                    return true;
                }
                if (event.getX() > getLeft() && event.getX() <= getLeft() + CLICK_RECT &&
                    event.getY() > getBottom() - CLICK_RECT && event.getY() <= getBottom()) {
                    //左下角 剪切
                    clickTime = System.currentTimeMillis();
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (event.getX() >= getRight() - CLICK_RECT && event.getX() <= getRight() &&
                        event.getY() >= getTop() && event.getY() <= getTop() + CLICK_RECT) {
                    //右上角 镜像
                    if (System.currentTimeMillis() - clickTime <= 1000) {
                        if (onClickIconListener != null) {
                            onClickIconListener.onClickIcon(this, ICON_RIGHT_TOP);
                        }
                        return true;
                    }
                }

                if (event.getX() >= getLeft() && event.getX() <= getLeft() + CLICK_RECT &&
                        event.getY() >= getTop() && event.getY() <= getTop() + CLICK_RECT) {
                    //左上角 删除
                    if (System.currentTimeMillis() - clickTime <= 1000) {
                        if (onClickIconListener != null) {
                            onClickIconListener.onClickIcon(this, ICON_LEFT_TOP);
                        }
                        return true;
                    }
                }
                if (event.getX() > getLeft() && event.getX() <= getLeft() + CLICK_RECT &&
                        event.getY() > getBottom() - CLICK_RECT && event.getY() <= getBottom()) {
                    //左下角 剪切
                    if (System.currentTimeMillis() - clickTime <= 1000) {
                        if (onClickIconListener != null) {
                            onClickIconListener.onClickIcon(this, ICON_LEFT_BOTTOM);
                        }
                        return true;
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mRectF.left = scaleBitmap.getWidth() / 2;
        mRectF.top = scaleBitmap.getHeight() / 2;
        mRectF.right = mWidth - scaleBitmap.getWidth() / 2;
        mRectF.bottom = mHeight - scaleBitmap.getHeight() / 2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasure(widthMeasureSpec);
        int measuredHeight = getMeasure(heightMeasureSpec);
        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    private int getMeasure(int measureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        switch (mode) {
            case MeasureSpec.AT_MOST:
                result = MeasureSpec.getSize(measureSpec);
                break;
            case MeasureSpec.EXACTLY:
                result = MeasureSpec.getSize(measureSpec);
                break;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(mRectF, mPaint);
        //右下角 缩放
        canvas.drawBitmap(scaleBitmap, mWidth - scaleBitmap.getWidth(), mHeight - scaleBitmap.getHeight(), null);
        //右上角 镜像
        canvas.drawBitmap(mirrorBitmap, mWidth - scaleBitmap.getWidth(), 0, null);
        //左上角 删除
        canvas.drawBitmap(deleteBitmap, 0, 0, null);
        //左下角 裁剪
        canvas.drawBitmap(cutBitmap, 0, mHeight - cutBitmap.getHeight(), null);
    }

    public void setOnClickIconListener(OnClickIconListener onClickIconListener) {
        this.onClickIconListener = onClickIconListener;
    }

    public interface OnClickIconListener {

        void onClickIcon(View view, int iconPosition);
    }
}
